package com.example.testdor.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.util.ArrayList;
import java.util.List;

public class MyView extends View {
    private String TAG = "MyView";
    private Paint paint;
    private Canvas mCanvas;
    private  int myX = 30;

    private List<DotValue> dotValueList;

    public MyView(Context context) {
        super(context);
        paint = new Paint();
        init();
    }

    public MyView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        paint = new Paint();
        init();
    }

    public MyView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        paint = new Paint();
        init();
    }

    private void init() {
        setBackgroundColor(Color.RED);
        dotValueList = new ArrayList<DotValue>();
        dotValueList.add(new DotValue(700, 200));
        dotValueList.add(new DotValue(700, 210));
        dotValueList.add(new DotValue(700, 220));
        dotValueList.add(new DotValue(400, 400));
        dotValueList.add(new DotValue(500, 400));

    }

    private void testListDraw() {
        Log.i(TAG, "testListDraw");
        for (DotValue it: dotValueList) {
            Log.i(TAG, "testListDraw list, x= " + it.mX + " y = " + it.mY);
        }
    }

    @Override
    protected void onDraw(@NonNull Canvas canvas) {
        super.onDraw(canvas);
        mCanvas = canvas;
        Log.i(TAG, "onDraw, myX = " + myX);

        for (DotValue it: dotValueList) {
            Log.i(TAG, "onDraw list, x= " + it.mX + " y = " + it.mY);
            canvas.drawCircle(it.mX, it.mY, 10, paint);
        }

        testListDraw();

//        invalidate();
    }

    private float mCircleX;
    private float mCircleY;
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                mCircleX = event.getX();
                mCircleY = event.getY();
                Log.i(TAG, "mCircleX = " + mCircleX + " mCircleY = " + mCircleY);
                dotValueList.add(new DotValue((int)mCircleX, (int)mCircleY));
                invalidate();
                break;
            default:
                break;
        }
        return true;
    }

    public void setPosition(int x, int y) {
        myX = x;
        Log.i(TAG, "setPosition, myX = " + myX);
        dotValueList.add(new DotValue(x, y));

        for (DotValue it: dotValueList) {
            Log.i(TAG, "setPosition list, x= " + it.mX + " y = " + it.mY);
        }


        invalidate(); // 重新绘制View
    }

    class DotValue {
        public int mX;
        public int mY;

        public DotValue(int x, int y) {
            mX = x;
            mY = y;
        }
    }
}
